const ProjectDetailsBranchService = require("../srevice/projectDetailsBranchService");
const ProjectDetailService = require("../srevice/projectDetailService");

const projectDetailsBranchService = new ProjectDetailsBranchService();
const projectDetailService = new ProjectDetailService();

const { projectDetailsBranchCreateError, projectDetailsGetError } = require("../main/err.type")
class projectDetailsBranchController {
    // 创建项目分支
    async createBranchProject(ctx) {
        try {
            // 主项目id，项目名称，项目周期，负责人
            const {
                projectId,
                branchId,
                projectName,
                projectLeader
            } = ctx.request.body;
            const getProjectRes = await projectDetailService.getProject({
               id: projectId
            });

            if (getProjectRes.length == 0) {
                ctx.body = projectDetailsGetError;
                return ctx.app.emit("error", projectDetailsGetError, ctx);
            } else {
                console.log('dsakjfgldsahjfdgslf');
                const res = await projectDetailsBranchService.createBranchProject({
                    id: projectId,
                    branchId,
                    projectName,
                    projectLeader
                });
                console.log(res);
                let sendMsg = {
                    code: 200,
                    data: '创建成功'
                }
                ctx.body = sendMsg
            }

        } catch (error) {
            console.log(error);
            ctx.body = projectDetailsBranchCreateError;
            return ctx.app.emit("error", projectDetailsBranchCreateError, ctx);
        }
    }

    // 获取项目分支
    async getBranchProject(ctx) {
        try {
            const {
                projectId
            } = ctx.query;
            const res = await projectDetailsBranchService.getBranchProject({
                projectId
            });
            console.log(res);
            let sendMsg = {
                code: 200,
                data: res
            }
            ctx.body = sendMsg
        } catch (error) {
            console.log(error);
            ctx.body = projectDetailsBranchCreateError;
            return ctx.app.emit("error", projectDetailsBranchCreateError, ctx);
        }
    }
    // 分页 获取项目分支
    async getBranchProjectByPage(ctx) {
        try {
            const {
                projectId,
                pageSize,
                currentPage
            } = ctx.query;
            const res = await projectDetailsBranchService.getBranchProjectByPage({
                projectId,
                pageSize,
                currentPage
            });
            console.log(res);
            let sendMsg = {
                code: 200,
                data: res
            }
            ctx.body = sendMsg
        } catch (error) {
            console.log(error);
            ctx.body = projectDetailsBranchCreateError;
            return ctx.app.emit("error", projectDetailsBranchCreateError, ctx);
        }
    }
    // 通过主项目id或者分支id获取项目分支
    async getBranchProjectById(ctx) {
        try {
            const {
                projectId,
                branchId
            } = ctx.query;
            const res = await projectDetailsBranchService.getBranchProjectById({
                projectId,
                branchId
            });
            console.log(res);
            let sendMsg = {
                code: 200,
                data: res
            }
            ctx.body = sendMsg
        } catch (error) {
            console.log(error);
            ctx.body = projectDetailsBranchCreateError;
            return ctx.app.emit("error", projectDetailsBranchCreateError, ctx);
        }
    }
}


module.exports = new projectDetailsBranchController(); 